package com.cskaoyan.controller.goods;

import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.model.vo.goods.wxgoods.WxGoodsDetailListVo;
import com.cskaoyan.model.vo.goods.wxgoods.WxGoodsListBySearchVo;
import com.cskaoyan.model.vo.goods.wxgoods.WxGoodsListVo;
import com.cskaoyan.model.vo.order.WxGoodsCategoryVo;
import com.cskaoyan.service.goods.WxGoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @program: project-market
 * @description:
 * @author: zhijie Weiwei
 * @create: 2021-12-04 19:08
 **/
@RequestMapping("wx/goods")
@RestController
public class WxGoodsController {
    @Autowired
    WxGoodsService wxGoodsService;


    /**
     * 有两种请求参数,对参数进行判断
     * @param keyword
     * @param categoryId
     * @param pageBo
     * @param session
     * @return
     */
    @GetMapping("list")
    public BaseRespVo wxGoodsList(String keyword, Integer categoryId, PageBo pageBo, Integer brandId) {
        //通过品类一级类目访问的
        if (categoryId!=null&&pageBo.getSort()==null) {
            WxGoodsListVo wxGoodsList = wxGoodsService.wxGoodsList(categoryId, pageBo.getPage(), pageBo.getLimit());
            return BaseRespVo.ok(wxGoodsList);
        } else if (brandId != null) {
            //通过品牌商id搜索访问
            WxGoodsListBySearchVo data = wxGoodsService.wxBrandId(brandId, pageBo);
            return BaseRespVo.ok(data);
        }        //通过搜索访问的
        //拿到用户id,判断用户是否登录,后续是否添加搜索历史纪录
        Integer id = (Integer) SecurityUtils.getSubject().getSession().getAttribute("uid");
        WxGoodsListBySearchVo data = wxGoodsService.wxGoodsListBySearch(keyword, categoryId, pageBo, id);
        return BaseRespVo.ok(data);
    }


    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public BaseRespVo detail(Integer id) {

        Integer uid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("uid");

        WxGoodsDetailListVo vo = wxGoodsService.detail(id,uid);

        return BaseRespVo.ok(vo);

    }


    /**
     * 查询相近商品
     *
     * @param id
     * @return
     */
    @GetMapping("related")
    public BaseRespVo related(Integer id) {

        PageListVo vo = wxGoodsService.relate(id);

        return BaseRespVo.ok(vo);

    }


    /**
     * @description: 传入类目id（L2），查找其父id的所有子类（L2），查找当前类目（L2），查找其父类目（L1）
     * @param:
     * @return:
     */
    @GetMapping("category")
    public BaseRespVo goodsCategory(Integer id){
        WxGoodsCategoryVo wxGoodsCategoryVo = wxGoodsService.goodsCategory(id);
        return BaseRespVo.ok(wxGoodsCategoryVo);
    }

    /**
     * 显示前台商品统计总数数
     * @return
     */
    @GetMapping("count")
    public BaseRespVo wxGoodsCount(){
        int count=wxGoodsService.wxGoodsCount();
        return BaseRespVo.ok(count);
    }
}
